package com.inspur.resourcequery.fragment;


import java.util.List;

import com.inspur.resourcequery.R;
import com.inspur.resourcequery.ResDetailActivity;
import com.inspur.resourcequery.ResListActivity;
import com.inspur.resourcequery.R.color;
import com.inspur.resourcequery.R.id;
import com.inspur.resourcequery.R.layout;
import com.inspur.resourcequery.model.ResData;
import com.inspur.resourcequery.model.ResRelationData;


import android.support.v4.app.Fragment;  
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Display; 
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class ResRelationFragment extends Fragment{
	public List resRelationList;
	private TableLayout resDetailLayout = null;
	View view = null;
	ResDetailActivity  activity;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.fragment_relation, null);		
		resDetailLayout =  (TableLayout)view.findViewById(R.id.relation_layout);
		activity= (ResDetailActivity)inflater.getContext();
//		activity.queryDetailData();
		resRelationList = activity.resRelationList;
		Display display = activity.getWindowManager().getDefaultDisplay();
		int width = display.getWidth();
		for (int i = 0; i < resRelationList.size(); i++) {
			ResRelationData obj = (ResRelationData)resRelationList.get(i);

			TableRow row = new TableRow(activity);
			row.setPadding(5, 5, 3, 5);
			
			TextView txtView_name = getTextView(activity);
			TextView txtView_value = getTextView(activity);
			txtView_name.setText(obj.getName());
			txtView_value.setText(obj.getShowvalue());

			txtView_name.setWidth((int) ((width) * 0.35));
			txtView_value.setWidth((int) ((width) * 0.55));
			
			txtView_name.setTextColor(Color.parseColor("#6381a2"));
			txtView_value.setTextColor(getResources().getColor(R.color.black));
			txtView_value.setGravity(Gravity.RIGHT);
			
			txtView_name.setContentDescription(obj.getRelationid());
			txtView_value.setContentDescription(obj.getRelationid());
			txtView_name.setClickable(true);
			txtView_name.setFocusable(true);
			
			txtView_name.setOnClickListener(relation_onclick_one);
			txtView_value.setOnClickListener(relationvalue_onclick_one);

			row.addView(txtView_name);
			row.addView(txtView_value);
			resDetailLayout.addView(row);
			
			View line = new View(activity);
			line.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, 1));
			line.setBackgroundColor(Color.parseColor("#e1e8f2"));
			resDetailLayout.addView(line);

		}
		return view;
	}
	private OnClickListener relation_onclick_one  = new OnClickListener() {
		public void onClick(View v) {
			TextView txt = (TextView)v;
			String str = txt.getContentDescription().toString();
			for (int i = 0; i < resRelationList.size(); i++) {
				ResRelationData obj =(ResRelationData) resRelationList.get(i);
				ResData data = new ResData();
				if(str.equals(obj.getRelationid())){
					if(obj.getType().equals("-1")){
						data.setResclassenname(obj.getRelated_class());
						data.setInt_id(obj.getAttr_value());
						Bundle bundle = new Bundle();
						bundle.putSerializable("data", data);
						Intent intent = new Intent();
						intent.setClass(activity,ResDetailActivity.class);
						intent.putExtras(bundle);
						startActivity(intent);
					}else if(obj.getType().equals("1")){
						Bundle bundle = new Bundle();
						bundle.putString("searchType", "searchByRelation");
						bundle.putSerializable("relation", obj);
						Intent intent = new Intent();
						intent.setClass(activity,ResListActivity.class);
						intent.putExtras(bundle);
						startActivity(intent);
					}
				}
			}
		}
	};
	private OnClickListener relationvalue_onclick_one  = new OnClickListener() {
		public void onClick(View v) {
			TextView txt = (TextView)v;
			String str = txt.getContentDescription().toString();
			for (int i = 0; i < resRelationList.size(); i++) {
				ResRelationData obj =(ResRelationData) resRelationList.get(i);
				ResData data = new ResData();
				if(str.equals(obj.getRelationid())){
					if(obj.getType().equals("-1")){
						data.setResclassenname(obj.getRelated_class());
						data.setInt_id(obj.getAttr_value());
						Bundle bundle = new Bundle();
						bundle.putSerializable("data", data);
						Intent intent = new Intent();
						intent.setClass(activity,ResDetailActivity.class);
						intent.putExtras(bundle);
						startActivity(intent);
					}else if(obj.getType().equals("1")){
						Bundle bundle = new Bundle();
						bundle.putString("searchType", "searchByRelation");
						bundle.putSerializable("relation", obj);
						Intent intent = new Intent();
						intent.setClass(activity,ResListActivity.class);
						intent.putExtras(bundle);
						startActivity(intent);
					}
				}
			}
		}
	};
	private TextView getTextView(Context context) {
		TextView textView = new TextView(context);
		textView.setPadding(10, 5, 5, 5);
		textView.setTextSize(16);
		return textView;
	}
}
